﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime;
using System.IO;

namespace IntegrationExample.Logic
{
    public class ForWinAPI
    {
        public static Icon GetSystemIcon(string _path)
        {
            WinAPI.Shfileinfo shinfo = new WinAPI.Shfileinfo();

            if (File.Exists(_path))
            {
                WinAPI.SHGetFileInfo(_path, 0, ref shinfo,
                    (uint)Marshal.SizeOf(shinfo),
                    WinAPI.Shgfi.SHGFI_ICON |
                    WinAPI.Shgfi.SHGFI_SMALLICON |
                    WinAPI.Shgfi.SHGFI_USEFILEATTRIBUTES |
                    WinAPI.Shgfi.SHGFI_SYSICONINDEX | WinAPI.Shgfi.SHGFI_OPENICON
                    | WinAPI.Shgfi.SHGFI_LARGEICON);

            }
            else
            {
                WinAPI.SHGetFileInfo(_path, 0x010, ref shinfo, (uint)Marshal.SizeOf(shinfo), 
                    WinAPI.Shgfi.SHGFI_ICON | WinAPI.Shgfi.SHGFI_SYSICONINDEX);
            }

            return Icon.FromHandle(shinfo.hIcon);
        }
    }
}
